<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/RanchBuddy.Master" Inherits="RanchBuddy.Web.Services.MyViewPage<List<Company>>" %>
<%@ Import Namespace="RanchBuddy.Core.Domain.Impl"%>
<%@ Import Namespace="RanchBuddy.Core.DataAccess.Impl"%>
<%@ Import Namespace="RanchBuddy.Core.Services.Impl"%>
<%@ Import Namespace="RanchBuddy.Web.Controllers"%>
<%@ Import Namespace="RanchBuddy.Core.Domain"%>
<%@ Import Namespace="RanchBuddy.Web.Services" %>
<%@ Import Namespace="Microsoft.Web.Mvc" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
	<title>Companies Near You</title>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script src="http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2" type="text/javascript"></script>
<script src="/Scripts/Map.js" type="text/javascript"></script>


    <fieldset>
        <legend>Companies near you</legend>
        <div style="float:left;padding-right:10px;">
    
    <%
        int lastCompanyTypeID = 0;
        foreach (Company c in Model)
        {
            if (lastCompanyTypeID != c.CompanyTypeID)
            {%>
                <b><%= new CompanyTypeService().GetCompanyTypeByID((c.CompanyTypeID ?? 0)).Name ?? ""%></b><BR />
            <% } %>
                
            <%= Html.ActionLink<CompanyController>(controller=>controller.Details(c.Name.Replace(" ","-")),c.Name) %><br />
            <%

            lastCompanyTypeID = (int)c.CompanyTypeID;
        } %>
        </div>
        <br />
        <div id="mapDiv"><div id="theMap"></div></div>
    </fieldset>
    <script type="text/javascript">        
        $(document).ready(function() {
            var latitude = <%= ((MapPoint)ViewData["MapPoint"]).Latitude %>;
            var longitude = <%= ((MapPoint)ViewData["MapPoint"]).Longitude %>;
            
            if((latitude == 0) || (longitude == 0))
                LoadMap();
            else
                LoadMap(latitude, longitude, mapLoaded);
        });
        
        function mapLoaded() {
            <%
            for(int i = 0; i<Model.Count; i++)
            {
                %>
                var pin<%=i %> = new VEShape(VEShapeType.Pushpin, new VELatLong(<%= Model[i].Latitude %>, <%= Model[i].Longitude %>));
                pin<%=i %>.SetTitle("<%= Model[i].Name %>");
                pin<%=i %>.SetDescription("<%= Model[i].Address1 %><BR><%= Model[i].Address2 %><BR><%= Model[i].City %>, <%= Model[i].State %> <%= Model[i].ZipCode %>");
                map.AddShape(pin<%=i %>);
                <%
            } %>
            map.SetZoomLevel(10);
        }
    </script>
</asp:Content>
